package com.metaera.app.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.metaera.app.MetaEraApplication;
import com.metaera.app.ui.activities.LoginActivity;
import com.metaera.app.utils.event.WeChatOAuthCallbackEvent;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.umeng.socialize.UMShareAPI;

import org.greenrobot.eventbus.EventBus;

import androidx.appcompat.app.AppCompatActivity;


public class WXEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {

    private static final int RETURN_MSG_TYPE_LOGIN = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MetaEraApplication.sWxApi.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        MetaEraApplication.sWxApi.handleIntent(intent, this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp resp) {
        //登录回调
        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                switch (resp.getType()) {
                    case RETURN_MSG_TYPE_LOGIN:
                        // 拿到了微信返回的code,立马再去请求access_token
                        String code = ((SendAuth.Resp) resp).code;
                        if (TextUtils.isEmpty(code)) {
                            code = getIntent().getStringExtra("_wxapi_sendauth_resp_token");
                        }

                        EventBus.getDefault().post(new WeChatOAuthCallbackEvent(code));
                        break;

                }
                break;
        }

        finish();
    }

}
